<?php
/**
 * RadioMorningController.class.php
 * 晨间电台控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-04-27
 */

namespace Admin\Controller;

class RadioMorningController extends BaseController {

	/**
	 * 电台列表获取
	 * @param 	$p
	 * @return 	json 	$list
	 */
	function getList(){
		$condition 	= $this->request('condition', []);
		$page 	= $this->request('p', 1);
		$pageHtml 	= true;

		$list 	= D('RadioMorning')->getList($condition, $page, $pageHtml);
		$this->response([
			'list'	=> $list,
			'page_html'	=> $pageHtml
		]);
	}

	/**
	 * 添加电台
	 * @param 	$title
	 * @param 	$banner
	 * @param 	$audio
	 * @return 	boolean
	 */
	function addItem(){
		$this->unEmptyParam(['title', 'banner', 'audio']);

		$title 	= $this->request('title');
		$banner	= $this->request('banner');
		$audio 	= $this->request('audio');
		$isTry 	= $this->request('is_try', 0);
		$onlineTime 	= $this->request('online_date') ? strtotime($this->request('online_date')) : time();
		$status 	= $this->request('online_date') ? 2 : 1;
		$content 	= $this->request('content');

		$banner 	= strchr($banner, '/server');
		$audio 	= strchr($audio, '/server');
		$audioTime 	= get_audio_info(strchr($audio, 'Uploads'), 'playtime_seconds');

		$radioId 	= D('RadioMorning')->add([
			'title'	=> $title,
			'banner'	=> $banner,
			'audio'	=> $audio,
			'audio_time'	=> $audioTime,
			'content'	=> $content,
			'is_try'	=> $isTry,
			'create_time'	=> time(),
			'online_time'	=> $onlineTime,
			'status'	=> $status
		]);

		$this->response();
	}

	/**
	 * 编辑电台
	 * @param 	$radio_id
	 * @param 	$radio_info
	 * @return 	boolean
	 */
	function updateItem(){
		$this->unEmptyParam(['radio_id', 'radio_info']);

		$radioId 	= $this->iInfo['radio_id'];
		$radioInfo 	= $this->iInfo['radio_info'];
		if($radioInfo['audio']){
			$radioInfo['audio']	= strchr($radioInfo['audio'], '/server');
			$radioInfo['audio_time']	= get_audio_info(strchr($radioInfo['audio'], 'Uploads'), 'playtime_seconds');
		}
		if($radioInfo['banner']){
			$radioInfo['banner']	= strchr($radioInfo['banner'], '/server');
		}
		$radioInfo['update_time']	= time();
		$result 	= D('RadioMorning')->where('id = %d', $radioId)->save($radioInfo);
		$this->response();
	}
	
}